import { getUserinfoApi } from '@/api';
import { UserBasic } from '@/types/auth';
import Taro from '@tarojs/taro';
import { toast } from '.';

export const getUser = (): UserBasic => Taro.getStorageSync('user');
export const setUser = data => Taro.setStorage({ key: 'user', data });

export const getToken = () => Taro.getStorageSync('token');
export const setToken = data => Taro.setStorage({ key: 'token', data });
// 新增的删除相关函数
export const clearUser = () => {
  Taro.removeStorageSync('user');
  Taro.removeStorageSync('token');
};

export function reloadUserInfo() {
  getUserinfoApi(getUser().ID)
    .then(res => {
      setUser(res);
    })
    .catch(() => toast('获取个人信息失败'));
}
